package com.mmz.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CuratorOperateDemo {
    public static void main(String[] args) {
        CuratorFramework curatorFramework = CuratorClientUtils.getInstance();
        System.out.println("连接成功");
//
//        try {
//            String result = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
//                    .forPath("/curator/curator1","c".getBytes());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        try {
//            curatorFramework.delete().deletingChildrenIfNeeded().forPath("/node");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        Stat stat = new Stat();
//        try {
//            byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/curator/curator1");
//            System.out.println(new String(bytes)+"---"+stat);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        try {
//            Stat stat = curatorFramework.setData().forPath("/curator","123".getBytes());
//            System.out.println(stat);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        ExecutorService service= Executors.newFixedThreadPool(1);
//        CountDownLatch countDownLatch=new CountDownLatch(1);
//        try {
//            curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).
//                    inBackground(new BackgroundCallback() {
//                        @Override
//                        public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
//                            System.out.println(Thread.currentThread().getName()+"->resultCode:"+curatorEvent.getResultCode()+"->"
//                            +curatorEvent.getType()+curatorEvent.getPath());
//
//                            countDownLatch.countDown();
//                        }
//                    },service).forPath("/mic","123".getBytes());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        try {
//            countDownLatch.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        service.shutdown();
//
//        try {
//            Collection<CuratorTransactionResult> resultCollections=curatorFramework.inTransaction().create().forPath("/trans1","123".getBytes()).and().
//                    create().forPath("/trans","31".getBytes()).and().commit();
//            for (CuratorTransactionResult result:resultCollections){
//                System.out.println(result.getForPath()+"->"+result.getType());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


    }
}
